package com.yxq.mdt.app.modules.socialcontact.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.appbar.AppBarLayout;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.BuildConfig;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.RouteTable;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.model.ShareBean;
import com.yxq.mdt.app.modules.bottomsheet.CommentBottomSheetDialog;
import com.yxq.mdt.app.modules.bottomsheet.ShareSheetDialog;
import com.yxq.mdt.app.modules.personal.bean.MainPersonalStateBean;
import com.yxq.mdt.app.modules.personal.bean.PublishResult;
import com.yxq.mdt.app.modules.picturebrowse.ui.PictureActivity;
import com.yxq.mdt.app.modules.socialcontact.adapter.CommentStateContactCircleAdapter;
import com.yxq.mdt.app.modules.socialcontact.adapter.ExamplePagerAdapter;
import com.yxq.mdt.app.modules.socialcontact.bean.BrowseListBean;
import com.yxq.mdt.app.modules.socialcontact.bean.GroupTopicListBean;
import com.yxq.mdt.app.modules.socialcontact.bean.SocialStateGiveListLikeBean;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialPageInfoContract;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialPgaeInfoPresenter;
import com.yxq.mdt.app.modules.socialcontact.ui.fragment.SocialStateBrowseFragment;
import com.yxq.mdt.app.modules.socialcontact.ui.fragment.SocialStateCommentFragment;
import com.yxq.mdt.app.modules.socialcontact.ui.fragment.SocialStateGiveLikeFragment;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.utils.MediaPlayerHelper;
import com.yxq.mdt.app.utils.ScaleTransitionPagerTitleView;
import com.yxq.mdt.app.widgets.NonSwipeableViewPager;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

/**
 * 动态详情
 */
@Route(path = RouteTable.ROUTER_ACTIVITY_TOPIC_DETAIL)
public class SocialStateDetailsAc extends BaseActivity<MainSocialPgaeInfoPresenter> implements MainSocialPageInfoContract.View {
    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.item_layout_nike_img)
    ImageView itemLayoutNikeImg;
    @BindView(R.id.item_layout_nike_name)
    TextView itemLayoutNikeName;
    @BindView(R.id.item_iv_sex)
    ImageView itemIvSex;
    @BindView(R.id.item_number_tv)
    TextView itemNumberTv;
    @BindView(R.id.vice_lint)
    LinearLayout viceLint;
    @BindView(R.id.item_layout_time)
    TextView itemLayoutTime;
    @BindView(R.id.item_layout_province)
    TextView itemLayoutProvince;
    @BindView(R.id.item_layout_attention)
    TextView itemLayoutAttention;
    @BindView(R.id.item_layout_content_development)
    TextView itemLayoutContentDevelopment;
    @BindView(R.id.publish_note)
    ImageView publishNote;
    @BindView(R.id.publish_add_voice_time)
    TextView publishAddVoiceTime;
    @BindView(R.id.publish_start_play)
    RelativeLayout publishStartPlay;
    @BindView(R.id.publish_save_voice)
    LinearLayout publishSaveVoice;
    @BindView(R.id.personal_details_banner)
    ConvenientBanner personalDetailsBanner;
    @BindView(R.id.personal_home_banner_lint)
    LinearLayout personalHomeBannerLint;
    @BindView(R.id.item_social_video_player)
    JzvdStd itemSocialVideoPlayer;
    @BindView(R.id.item_social_lint)
    LinearLayout itemSocialLint;
    @BindView(R.id.item_tv_subject)
    TextView itemTvSubject;
    @BindView(R.id.item_tv_subject_lint)
    LinearLayout itemTvSubjectLint;
    @BindView(R.id.item_layout_content_recyclerView)
    RecyclerView itemLayoutContentRecyclerView;
    @BindView(R.id.item_layout_circle_img)
    ImageView itemLayoutCircleImg;
    @BindView(R.id.group_name)
    TextView groupName;
    @BindView(R.id.group_lint)
    LinearLayout groupLint;
    @BindView(R.id.dynamic_list_lint)
    LinearLayout dynamicListLint;
    @BindView(R.id.social_state_indicator)
    MagicIndicator socialStateIndicator;
    @BindView(R.id.app_bar)
    AppBarLayout appBar;
    @BindView(R.id.video_view_pager)
    NonSwipeableViewPager videoViewPager;
    @BindView(R.id.social_state_btn)
    TextView socialStateBtn;
    @BindView(R.id.social_state_chat)
    LinearLayout socialStateChat;
    @BindView(R.id.social_state_give_tv)
    TextView socialStateGiveTv;
    @BindView(R.id.item_layout_content_browse_img)
    ImageView itemLayoutContentBrowseImg;
    @BindView(R.id.social_state_add_img)
    ImageView socialStateAddImg;
    @BindView(R.id.social_state_add)
    LinearLayout socialStateAdd;
    @BindView(R.id.social_state_bottom_layout)
    LinearLayout socialStateBottomLayout;
    @BindView(R.id.item_layout_content_browse_img_lint)
    LinearLayout itemLayoutContentBrowseImgLint;
    @BindView(R.id.report)
    TextView report;
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    private List<Fragment> fragments;
    private ExamplePagerAdapter examplePagerAdapter;
    private FragmentContainerHelper fragmentContainerHelper;
    private String topicId = "";
    List<String> mDatList;
    CommentStateContactCircleAdapter stateContactAdapter;
    private String content = "#话题#";//话题
    private String contentTv = "", id = "";
    SpannableStringBuilder spannableStringBuilder;
    CountDownTimer countDownTimer;
    CommentBottomSheetDialog commentBottomSheetDialog;
    FragmentManager comment;
    SocialStateCommentFragment socialStateCommentFragment;
    SocialStateGiveLikeFragment socialStateGiveLikeFragment;
    SocialStateBrowseFragment socialStateBrowseFragment;
    String viceTime;
    CommonNavigator commonNavigator;
    int num = 0;
    String[] CHANNELS;
    int giveNum;
    public boolean isVoiceStart = false;

    @Override
    public int getLayoutId() {
        return R.layout.activity_sociall_state_details;
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        topicId = getIntent().getStringExtra("topicId");
        id = getIntent().getStringExtra("id");
        comment = getSupportFragmentManager();
        if (!TextUtils.isEmpty(id) && topicId == null) {
            topicId = id;
        }
        personalDetailsBanner.setPageIndicator(new int[]{R.drawable.shape_banner_selector_unchecked, R.drawable.shape_banner_selector_checked});
    }

    protected void convert(GroupTopicListBean item) {
        MainSocialContactModel.setSaveViewParameter("2", item.getId() + "", "4", new ApiModel.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {

            }

            @Override
            public void onError(String errMsg) {
            }
        });
        if (item.getContent() != null || !"".equals(item.getContent())) {
            itemTvSubjectLint.setVisibility(View.VISIBLE);
            contentTv = item.getContent();
        } else {
            itemTvSubjectLint.setVisibility(View.GONE);
        }
        if (item.getContent() != null && item.getContent().contains("#")) {
            content = (item.getContent().substring(item.getContent().indexOf("#"), item.getContent().lastIndexOf("#") + 1));
        } else {
            content = item.getContent();
        }

        spannableStringBuilder = new SpannableStringBuilder("");

        if (!TextUtils.isEmpty(contentTv)) {
            spannableStringBuilder.append(contentTv);
        }
        report.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showBottomToast("该功能正在开发中!");
            }
        });
        if (item.getContent() != null && item.getContent().contains("#")) {
            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#FFD800"));
            spannableStringBuilder.setSpan(foregroundColorSpan, 0, (item.getContent().substring(item.getContent().indexOf("#"), item.getContent().lastIndexOf("#") + 1)).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#222222")), (item.getContent().substring(item.getContent().indexOf("#"), item.getContent().lastIndexOf("#") + 1)).length(), spannableStringBuilder.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(@NonNull View view) {
                    if (countDownTimer != null) {
                        countDownTimer.onFinish();
                    }
                    Jzvd.releaseAllVideos();
                    MediaPlayerHelper.realese();
                    Intent it = new Intent(mActivity, CircleTopicAc.class);
                    it.putExtra("topicId", item.getId());
                    it.putExtra("topic", (item.getContent().substring(item.getContent().indexOf("#"), item.getContent().lastIndexOf("#") + 1)));
                    mActivity.startActivity(it);
                }
            };
            //文本可点击，有点击事件
            spannableStringBuilder.setSpan(clickableSpan, 0, content.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            // 设置此方法后，点击事件才能生效
            itemTvSubject.setMovementMethod(LinkMovementMethod.getInstance());
            itemTvSubject.setText(spannableStringBuilder);
        } else {
            itemTvSubject.setText(item.getContent());
            itemTvSubject.setTextColor(Color.parseColor("#222222"));
            itemTvSubject.setTextColor(Color.parseColor("#222222"));
        }
        //点赞
        socialStateGiveTv.setText(item.getUpCount() == 0 ? "点赞" : item.getUpCount() + "");

        if (item.getCollect() != 0) {
            socialStateAddImg.setImageResource(R.mipmap.icon_voice_stars_yellow);
        } else {
            socialStateAddImg.setImageResource(R.mipmap.icon_voice_stars_gray);
        }
        num = item.getCollect();
        //头像
        GlideHelper.setLoadRoundImage(mActivity, item.getHeadimg(), R.mipmap.head_icon, itemLayoutNikeImg);
        //昵称
        itemLayoutNikeName.setText(item.getUserName());
        //发布时间
        itemLayoutTime.setText(TimeUtils.getFriendlyTimeSpanByNow(item.getCreateDateOld(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
        //性别
        itemIvSex.setImageResource(item.getSex() == 1 ? R.mipmap.xb_nan : R.mipmap.xb_nv);
        if (item.getGiveLike() != 0) {
            itemLayoutContentBrowseImg.setImageResource(R.mipmap.zan);
        } else {
            itemLayoutContentBrowseImg.setImageResource(R.mipmap.icon_community_fabulous);
        }
        if (item.getGroupName() != null) {
            groupName.setText(item.getGroupName());
        } else {
            groupLint.setVisibility(View.GONE);
        }
        //是否显示//是否为本人
        if (item.getConcern() != 0 || item.getRelationHideButton().equals("0")) {
            itemLayoutAttention.setVisibility(View.GONE);
        } else {
            itemLayoutAttention.setVisibility(View.VISIBLE);
        }
        itemLayoutAttention.setOnClickListener(view -> MainSocialContactModel.setInterestRelation(item.getCreateMan() + "", 1, 1, new ApiModel.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                showCenterToast("关注成功");
                itemLayoutAttention.setVisibility(View.GONE);
            }

            @Override
            public void onError(String errMsg) {
                showCenterToast("关注失败");
            }
        }));
        groupLint.setOnClickListener(view -> {
            if (countDownTimer != null) {
                countDownTimer.onFinish();
            }
            Jzvd.releaseAllVideos();
            MediaPlayerHelper.realese();
            Intent intent = new Intent(mActivity, CircleTheHomepageAc.class);
            intent.putExtra("id", item.getGroupId());
            intent.putExtra("CircleName", item.getGroupName());
            startActivity(intent);
        });
        int audioLen = 0;
        if (item.getIntroductionAudioFile() != null && item.getIntroductionAudioFile().size() != 0) {
            viceLint.setVisibility(View.VISIBLE);
            audioLen = item.getIntroductionAudioFile().get(0).getFileLength();
            if (audioLen < 1000) {
                itemNumberTv.setText(item.getIntroductionAudioFile().get(0).getFileLength() + "″");
            } else {
                audioLen = audioLen / 1000;
                itemNumberTv.setText(audioLen + "″");
            }
            int finalAudioLen = audioLen;
            viceLint.setOnClickListener(view -> {
                if (isVoiceStart) {
                    isVoiceStart = false;
                    MediaPlayerHelper.realese();
                    countDownTimer.onFinish();
                } else {
                    isVoiceStart = true;
                    viceTime = finalAudioLen + "";
                    MediaPlayerHelper.playSound(item.getIntroductionAudioFile().get(0).getFilePath(), mediaPlayer -> isVoiceStart = false);
                    getTime(viceTime, null, itemNumberTv);
                    countDownTimer.start();
                }
            });
        } else {
            viceLint.setVisibility(View.GONE);
        }
        if (item.getGroupName() != null) {
            groupName.setText(item.getGroupName());
            GlideHelper.setLoadRoundImage(mActivity, item.getGroupLogo(), R.mipmap.head_icon, itemLayoutCircleImg);
        } else {
            groupName.setVisibility(View.GONE);
        }
        itemLayoutNikeImg.setOnClickListener(view -> {
            if (countDownTimer != null) {
                countDownTimer.onFinish();
            }
            Jzvd.releaseAllVideos();
            MediaPlayerHelper.realese();
            Intent it = new Intent(mActivity, SocialPersonalHomepageAc.class);
            it.putExtra("userId", item.getCreateMan());
            startActivity(it);
        });
        giveNum = item.getUpCount();
        int len = 0;
        if (item.getTopicFileList() != null && item.getTopicFileList().size() > 0) {
            switch (item.getClass1()) {
                case "1":
                    itemLayoutContentRecyclerView.setVisibility(View.VISIBLE);
                    itemSocialLint.setVisibility(View.GONE);
                    publishSaveVoice.setVisibility(View.GONE);
                    personalHomeBannerLint.setVisibility(View.VISIBLE);
                    initGoodsImages(item);
                    break;
                case "2":
                    itemSocialLint.setVisibility(View.VISIBLE);
                    itemLayoutContentRecyclerView.setVisibility(View.GONE);
                    publishSaveVoice.setVisibility(View.GONE);
                    personalHomeBannerLint.setVisibility(View.GONE);
                    ImageView imageViewVideo = itemSocialVideoPlayer.thumbImageView;
                    imageViewVideo.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    GlideHelper.setLoadRoundImage(mActivity, item.getTopicFileList().get(0).getFile(), R.color.gray, imageViewVideo);
                    itemSocialVideoPlayer.setUp(item.getTopicFileList().get(0).getFile(), "", itemSocialVideoPlayer.SCREEN_NORMAL);
                    itemSocialVideoPlayer.setVideoImageDisplayType(JzvdStd.VIDEO_IMAGE_DISPLAY_TYPE_FILL_SCROP);
                    break;
                case "6"://语音
                    itemLayoutContentRecyclerView.setVisibility(View.GONE);
                    itemSocialLint.setVisibility(View.GONE);
                    publishSaveVoice.setVisibility(View.VISIBLE);
                    personalHomeBannerLint.setVisibility(View.GONE);
                    if (item.getTopicFileList().get(0).getClass2() != null) {
                        len = Integer.parseInt(item.getTopicFileList().get(0).getClass2());
                        if (len >= 1000) {
                            len = len / 1000;
                        }
                        publishAddVoiceTime.setText(len + "″");
                    }
                    int finalLen = len;
                    publishSaveVoice.setOnClickListener(view -> {
                        MediaPlayerHelper.playSound(item.getTopicFileList().get(0).getFile());
                        getTime(finalLen + "", publishNote, publishAddVoiceTime);
                        countDownTimer.start();
                    });
                    break;
                default:
            }
        } else {
            itemLayoutContentRecyclerView.setVisibility(View.GONE);
            itemSocialLint.setVisibility(View.GONE);
            publishSaveVoice.setVisibility(View.GONE);
            itemTvSubjectLint.setVisibility(View.VISIBLE);
            personalHomeBannerLint.setVisibility(View.GONE);
        }
    }


    /**
     * @param playTime  开始时间
     * @param imageView 获取当前控件
     * @param timeTv    获取显示秒数
     */
    private void getTime(String playTime, ImageView imageView, TextView timeTv) {
        if (imageView != null) {
            imageView.setImageResource(R.mipmap.voice_note_new);
        }

        countDownTimer = new CountDownTimer(Integer.parseInt(playTime) * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeTv.setText(millisUntilFinished / 1000 + "″");
            }

            @Override
            public void onFinish() {
                timeTv.setText(Integer.parseInt(playTime) + "″");
                if (imageView != null) {
                    imageView.setImageResource(R.mipmap.voice_note);
                }
                countDownTimer.cancel();
            }
        };
    }


    private void initMagicIndicator4() {
        commonNavigator = new CommonNavigator(mActivity);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDatList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context); //使用自定义
                simplePagerTitleView.setNormalColor(Color.parseColor("#999999"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#222222"));
                simplePagerTitleView.setText(mDatList.get(index));
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setOnClickListener(v -> {
                    videoViewPager.setCurrentItem(index);
//                    switchPages(index);
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });
        socialStateIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return UIUtil.dip2px(mActivity, 0);
            }
        });
        final FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(socialStateIndicator);
        fragmentContainerHelper.setInterpolator(new OvershootInterpolator(0.1f));
        fragmentContainerHelper.setDuration(0);
        videoViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                fragmentContainerHelper.handlePageSelected(position);
            }
        });
        videoViewPager.setOffscreenPageLimit(3);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new MainSocialPgaeInfoPresenter(this, new MainSocialContactModel());
        mPresenter.getPageInfoList(String.valueOf(1), String.valueOf(30), topicId);
        mPresenter.getTopicById(topicId);
    }

    /**
     * 初始化
     */
    private void initFragments() {
        onNumberChangeListener = (comment, praise) -> {
            hideLoadingDialog();
            if (comment != -1) {
                CHANNELS = new String[]{"评论" + "(" + comment + ")", CHANNELS[1], "最近访问"};
                mDatList = Arrays.asList(CHANNELS);
                socialStateIndicator.getNavigator().notifyDataSetChanged();
            }
            if (praise != -1) {
                CHANNELS = new String[]{CHANNELS[0], "点赞" + "(" + praise + ")", "最近访问"};
                mDatList = Arrays.asList(CHANNELS);
                socialStateIndicator.getNavigator().notifyDataSetChanged();
//                Log.e("刷新标题", "点赞数目" + praise);
            }

        };
        //评论
        socialStateCommentFragment = new SocialStateCommentFragment(onNumberChangeListener);
        Bundle commentBundle = new Bundle();
        commentBundle.putString("topicId", topicId);
        socialStateCommentFragment.setArguments(commentBundle);
        fragments.add(socialStateCommentFragment);

        //点赞
        socialStateGiveLikeFragment = new SocialStateGiveLikeFragment(onNumberChangeListener);
        Bundle likeBundle = new Bundle();
        likeBundle.putString("topicId", topicId);
        socialStateGiveLikeFragment.setArguments(likeBundle);
        fragments.add(socialStateGiveLikeFragment);

        //浏览
        socialStateBrowseFragment = new SocialStateBrowseFragment();
        Bundle browsBundle = new Bundle();
        browsBundle.putString("topicId", topicId);
        browsBundle.putString("id", id);
        socialStateBrowseFragment.setArguments(browsBundle);
        fragments.add(socialStateBrowseFragment);
    }


    @Override
    public void onLoadPageInfoSuccess(MainPersonalStateBean mainPersonalStateBean) {
    }

    @Override
    public void onLoadPageInfoFailed() {

    }

    GroupTopicListBean groupTopicListBean;

    @Override
    public void onLoadTopicByIdSuccess(GroupTopicListBean groupTopicListBean) {
        this.groupTopicListBean = groupTopicListBean;
        if (groupTopicListBean != null) {
            CHANNELS = new String[]{"评论" + "(" + groupTopicListBean.getCommentCount() + ")", "点赞" + "(" + groupTopicListBean.getUpCount() + ")", "最近访问"};
            mDatList = Arrays.asList(CHANNELS);
            fragments = new ArrayList<>();
            initFragments();
            examplePagerAdapter = new ExamplePagerAdapter(getSupportFragmentManager(), fragments, mDatList);
            videoViewPager.setAdapter(examplePagerAdapter);
            fragmentContainerHelper = new FragmentContainerHelper();
            fragmentContainerHelper.handlePageSelected(0, false);
            initMagicIndicator4();
            convert(groupTopicListBean);
            id = groupTopicListBean.getId();
            num = groupTopicListBean.getCollect();
        }
    }

    @Override
    public void onLoadTopicByIdFailed() {
    }

    @Override
    public void onLoadGiveFailed() {
    }

    @Override
    public void onLoadGiveSuccess(SocialStateGiveListLikeBean socialStateGiveListLikeBean) {
    }

    @Override
    public void onLoadBrowseListFailed() {
    }

    @Override
    public void onLoadBrowseListSuccess(BrowseListBean socialStateGiveListLikeBean) {
    }


    @Override
    public void onLoadTopicFavorSuccess(String s) {
    }

    @Override
    public void onLoadTopicFavorFailed() {
    }


    @OnClick({R.id.social_state_share, R.id.item_layout_content_browse_img_lint, R.id.social_state_btn, R.id.social_state_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.social_state_btn:
                //评论
                commentBottomSheetDialog = new CommentBottomSheetDialog(socialStateCommentFragment, topicId, "", 0);
                commentBottomSheetDialog.show(comment, "commentBottomSheetDialog");
                break;
            case R.id.social_state_add:
                //收藏
                if (num == 0) {
                    MainSocialContactModel.saveGsTopicFavour(topicId, String.valueOf(3), new ApiModel.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String s) {
                            showBottomToast("收藏成功");
                            getGive();
                        }

                        @Override
                        public void onError(String errMsg) {
                            showBottomToast("收藏失败");
                        }
                    });
                } else {
                    MainSocialContactModel.delGsTopicFavour(String.valueOf(3), topicId, new ApiModel.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String s) {
                            showBottomToast("取消成功");
                            getGive();
                        }

                        @Override
                        public void onError(String errMsg) {
                            showBottomToast("收藏失败");
                        }
                    });
                }
                break;
            case R.id.item_layout_content_browse_img_lint:
                //点赞
                if (groupTopicListBean == null) {
                    return;
                }
                getGiveLike(groupTopicListBean);
                break;
            case R.id.social_state_share:
                //分享
                if (groupTopicListBean == null) {
                    return;
                }
                ShareBean shareBean = new ShareBean();

                if (!TextUtils.isEmpty(groupTopicListBean.getContent())) {
                    shareBean.setTitle(groupTopicListBean.getContent());//文本
                    shareBean.setText(groupTopicListBean.getContent());//文本
                } else {
                    shareBean.setTitle("社交电商新玩法，好玩让你意想不到。");//文本
                    shareBean.setText("社交电商新玩法，好玩让你意想不到。");//文本
                }
                shareBean.setShareType("0");//图文分享
                shareBean.setShareTypeAPP(2);
                shareBean.setHandleId(groupTopicListBean.getId());
                if (groupTopicListBean.getTopicFileList() != null && groupTopicListBean.getTopicFileList().size() > 0 && "1".equals(groupTopicListBean.getTopicFileList().get(0).getClass1())) {
                    shareBean.setImageUrl(groupTopicListBean.getTopicFileList().get(0).getFile());//LOGO
                } else {
                    shareBean.setImageUrl("https://yxqoss.oss-cn-hangzhou.aliyuncs.com/gs_app/init/727c432c9e186a11.png");//LOGO
                }
                if (!"release".equals(BuildConfig.RELEASE)) {
                    shareBean.setUrl("http://testpm.quai69.com/webtrends.html?topicId=" + groupTopicListBean.getId() + "&platform=2&parent_id=" + getParentId());//分享URL
                } else {
                    shareBean.setUrl("http://pm.quai69.com/webtrends.html?topicId=" + groupTopicListBean.getId() + "&platform=2&parent_id=" + getParentId());
                }
                ShareSheetDialog shareSheetDialog = new ShareSheetDialog(shareBean, false);
                shareSheetDialog.show(getSupportFragmentManager(), "shareSheetDialog");
                break;
        }
    }

    /**
     * 初始化个人主页轮播图
     *
     * @param
     */
    private void initGoodsImages(GroupTopicListBean groupTopicListBean) {
        personalDetailsBanner.setPages(new CBViewHolderCreator() {
            @Override
            public Holder createHolder(View itemView) {
                return new ImageHolderView(itemView);
            }

            @Override
            public int getLayoutId() {
                return R.layout.layout_banner_datails_item;
            }
        }, groupTopicListBean.getTopicFileList());
        personalDetailsBanner.startTurning();
        personalDetailsBanner.setOnItemClickListener(position -> {
            if (countDownTimer != null) {
                countDownTimer.onFinish();
            }
            Jzvd.releaseAllVideos();
            MediaPlayerHelper.realese();
            //预览
            Intent intent = new Intent(mActivity, PictureActivity.class);
            intent.putExtra("position", position);
            ArrayList<String> images = new ArrayList<>();
            for (GroupTopicListBean.TopicFileListBean topicFileListBean : groupTopicListBean.getTopicFileList()) {
                images.add(topicFileListBean.getFile());
            }
            intent.putExtra("images", images);
            startActivity(intent);
        });
    }

    /**
     * 点赞
     *
     * @param groupTopicListBean
     */
    private void getGiveLike(GroupTopicListBean groupTopicListBean) {
        itemLayoutContentBrowseImgLint.setEnabled(false);
        switch (groupTopicListBean.getGiveLike()) {
            case 0:
                showLoadingDialog(false);
                MainSocialContactModel.saveGsTopicFavour(groupTopicListBean.getId(), "2", new ApiModel.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String s) {
                        hideLoadingDialog();
                        itemLayoutContentBrowseImg.setImageResource(R.mipmap.zan);
                        groupTopicListBean.setGiveLike(1);
                        groupTopicListBean.setUpCount((groupTopicListBean.getUpCount() + 1));
                        socialStateGiveTv.setText(groupTopicListBean.getUpCount() == 0 ? "点赞" : groupTopicListBean.getUpCount() + "");
                        showCenterToast("点赞成功");
                        socialStateGiveLikeFragment.refresh();
                        if (onNumberChangeListener != null) {
                            onNumberChangeListener.onChange(-1, groupTopicListBean.getUpCount());
                        }
                        itemLayoutContentBrowseImgLint.setEnabled(true);
                        getGive();
                    }

                    @Override
                    public void onError(String errMsg) {
                        hideLoadingDialog();
                        showCenterToast("点赞失败");
                    }
                });
                break;
            case 1:
                showLoadingDialog(false);
                MainSocialContactModel.delGsTopicFavour("2", groupTopicListBean.getId(), new ApiModel.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String s) {
                        hideLoadingDialog();
                        itemLayoutContentBrowseImg.setImageResource(R.mipmap.icon_community_fabulous);
                        groupTopicListBean.setGiveLike(0);
                        groupTopicListBean.setUpCount((groupTopicListBean.getUpCount() - 1));
                        socialStateGiveTv.setText(groupTopicListBean.getUpCount() == 0 ? "点赞" : groupTopicListBean.getUpCount() + "");
                        showCenterToast("取消点赞成功");
                        socialStateGiveLikeFragment.refresh();
                        if (onNumberChangeListener != null) {
                            onNumberChangeListener.onChange(-1, groupTopicListBean.getUpCount());
                        }
                        itemLayoutContentBrowseImgLint.setEnabled(true);
                        getGive();
                    }

                    @Override
                    public void onError(String errMsg) {
                        hideLoadingDialog();
                        showCenterToast("取消点赞失败");
                    }
                });
                break;
        }

    }


    /**
     * 图片轮转实现
     */
    class ImageHolderView extends Holder<GroupTopicListBean.TopicFileListBean> {
        private ImageView imageView;

        public ImageHolderView(View itemView) {
            super(itemView);
        }

        @Override
        protected void initView(View itemView) {
            imageView = itemView.findViewById(R.id.banner_image);
//            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }

        @Override
        public void updateUI(GroupTopicListBean.TopicFileListBean bean) {
            if (bean.getFile() == null) {
                GlideHelper.setLoadRoundImage(mActivity, bean.getFile(), R.mipmap.placeholder_noraml, imageView);
            } else {
                GlideHelper.setLoadRoundImage(mActivity, bean.getFile(), R.mipmap.placeholder_noraml, imageView);
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.onFinish();
        }
        Jzvd.releaseAllVideos();
        MediaPlayerHelper.realese();
        ToastUtils.cancel();
    }

    /**
     * 获取点赞状态
     */
    private void getGive() {
        MainSocialContactModel.getTopicById(id, new ApiModel.CommonCallback<GroupTopicListBean>() {
            @Override
            public void onSuccess(GroupTopicListBean gh) {
                groupTopicListBean = gh;
                convert(groupTopicListBean);

                if (groupTopicListBean.getCollect() != 0) {
                    socialStateAddImg.setImageResource(R.mipmap.icon_voice_stars_yellow);
                } else {
                    socialStateAddImg.setImageResource(R.mipmap.icon_voice_stars_gray);
                }

                if (groupTopicListBean.getGiveLike() != 0) {
                    itemLayoutContentBrowseImg.setImageResource(R.mipmap.zan);
                } else {
                    itemLayoutContentBrowseImg.setImageResource(R.mipmap.icon_community_fabulous);
                }
                socialStateGiveTv.setText(groupTopicListBean.getUpCount() == 0 ? "点赞" : groupTopicListBean.getUpCount() + "");
            }

            @Override
            public void onError(String errMsg) {

            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        Jzvd.goOnPlayOnPause();
        MediaPlayerHelper.realese();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    OnNumberChangeListener onNumberChangeListener;

    public interface OnNumberChangeListener {
        void onChange(int comment, int praise);
    }
}
